package com.i18n.plugin.services

import ai.grazie.nlp.patterns.RegexPattern
import com.i18n.plugin.core.RegexFilePathMatcher
import com.intellij.lang.LanguageParserDefinitions
import com.intellij.openapi.externalSystem.view.ProjectNode
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.roots.ProjectRootUtil
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vcs.changes.ignore.util.RegexUtil
import com.intellij.openapi.vfs.*
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.ui.tree.project.ProjectFileTreeModel
import org.apache.commons.collections.iterators.EmptyListIterator
import org.apache.commons.io.filefilter.FileFilterUtils
import org.apache.logging.log4j.LogManager
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.*


class MyProjectService(project: Project) {

    init {
        val regex = ".*.json"
        try {
            val i18nFiles = mutableListOf<VirtualFile>()
            val regexFilePathMatcher = RegexFilePathMatcher(regex)

            val contentRoots = ProjectRootManager.getInstance(project).contentRoots
            for (root in contentRoots) {

                VfsUtilCore.visitChildrenRecursively(root, object : VirtualFileVisitor<Unit>() {
                    override fun visitFile(file: VirtualFile): Boolean {
                        if (regexFilePathMatcher.matches(file.path)) {
                            i18nFiles.add(file)
                        }
                        // 返回true表示继续遍历子节点
                        return true
                    }
                })
            }
            FileEditorManager.getInstance(project).openFile(i18nFiles[0], true)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }


}
